package com.culturalCenter.dataCenter.Utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.function.Function;

/**
 * @Author zhao
 * @Date 2021-3-31 15:49
 * 时间处理工具
 */
public class TimeUtil {

    /**
     * 获取上个月的年份和月份
     *
     * @return
     */
    public static String getLastMonthYearAndMonth() {
        LocalDate today = LocalDate.now();
        today = today.minusMonths(1);
        DateTimeFormatter formatters = DateTimeFormatter.ofPattern("yyyy-MM");
        return formatters.format(today);
    }

    /**
     * 获取特定时间（格式：yyyy-MM-dd HH）的上个月统一时间点
     *
     * @param date
     *
     * @return
     */
    public static String getTheDateLastMonth(String date) {
        LocalDateTime newDate = LocalDateTime.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd HH"));
        newDate = newDate.minusMonths(1);
        DateTimeFormatter formatters = DateTimeFormatter.ofPattern("yyyy-MM-dd HH");
        return formatters.format(newDate);
    }

    /**
     * 判断当前时间距离第二天凌晨的秒数
     *
     * @return 返回值单位为[s:秒]
     */
    public static Long getSecondsNextEarlyMorning() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_YEAR, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return (cal.getTimeInMillis() - System.currentTimeMillis()) / 1000;
    }

    /**
     * 获取当前传入时间的开始时间和结束时间
     * @param date
     * @param type
     * @return
     */

    public  static Map<String ,String>  getTimeMap(Date date,String type){
        LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        Map<String ,String> timeMap = new HashMap<>();
        switch (type){
            case "year":
                int year = localDate.getYear();
                LocalDate startOfYear = Year.of(year).atDay(1);
                LocalDate endOfYear = Year.of(year).atMonth(12).atEndOfMonth();
                timeMap.put("start",startOfYear+" 00:00:00");
                timeMap.put("end",endOfYear+" 23:59:59");
                break;
            case "month":
                YearMonth yearMonth = YearMonth.from(localDate);
                LocalDate startOfMonth = yearMonth.atDay(1);
                LocalDate endOfMonth = yearMonth.atEndOfMonth();
                timeMap.put("start",startOfMonth+" 00:00:00");
                timeMap.put("end",endOfMonth+" 23:59:59");
                break;
            case "day":
                LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
                LocalDate localTime =localDateTime.toLocalDate();
                LocalDateTime startOfDay = localTime.atStartOfDay();
                LocalDateTime endOfDay = localTime.atTime(LocalTime.MAX);
                String startTime = startOfDay.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                String endTime = endOfDay.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                timeMap.put("start",startTime);
                timeMap.put("end",endTime);
                break;
        }
        return timeMap;
    }

    /**
     * 通过不同格式时间进行排序
     * @param list
     * @param stringFunction
     * @param format
     * @param <T>
     */
    public static  <T>  void timeSort(List<T> list, Function<T,String> stringFunction, String format){
        list.sort((v1,v2)->{
            try{
                SimpleDateFormat sdf = new SimpleDateFormat(format);
                Date date1 = sdf.parse(stringFunction.apply(v1));
                Date date2 = sdf.parse(stringFunction.apply(v2));
                return date1.compareTo(date2);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return 0;
        });
    }

}
